package com.pwp.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.pwp.vo.ScheduleDateTag;
import com.pwp.vo.ScheduleVO;

/**
 * 对日程DAO操作
 * @author jack_peng
 *
 */
public class ScheduleDAO {

	private DatabaseManager manager = null;
	//private Context context = null;
	
	public ScheduleDAO(Context context){

		//this.context = context;
		manager = new DatabaseManager(context);
	}
	
	public int saveOrUpdate(ScheduleVO scheduleVO){
		if(0!=scheduleVO.getScheduleID()){
			return update(scheduleVO);
		}else{
			return save(scheduleVO);
		}
		
	}
	
	public List<ScheduleVO> searchByCondition(Map condition){
		    int searchType=Integer.valueOf(condition.get("searchType").toString());
		    List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
			SQLiteDatabase db = manager.getWritableDatabase();
			Cursor cursor =null;
			
		    if (searchType == 0){
		    	cursor= db.query("t_schedule",formatSechduleColumn(), "schedule_title like '%"+condition.get("title").toString()+"%'", new String[]{}, null, null, null);
		    }
			if (searchType == 1) {
				cursor= db.query("t_schedule",formatSechduleColumn(), "schedule_content like '%"+condition.get("content").toString()+"%'", new String[]{}, null, null, null);
			}
			if (searchType == 2) {
				cursor= db.query("t_schedule",formatSechduleColumn(), "schedule_status=? ", new String[]{condition.get("status").toString()}, null, null, null);
			}
			if (searchType == 3) {
				cursor= db.query("t_schedule",formatSechduleColumn(), "schedule_start_date >= '"+condition.get("startTime").toString()+"' and schedule_start_date <= '"+condition.get("endTime").toString()+"' ", new String[]{}, null, null, null);
			}
			while(cursor.moveToNext()){
				ScheduleVO o=new ScheduleVO(
						cursor.getInt(cursor.getColumnIndex("schedule_id")),
						cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
						cursor.getInt(cursor.getColumnIndex("remindID")),
						cursor.getString(cursor.getColumnIndex("schedule_title")),
						cursor.getInt(cursor.getColumnIndex("schedule_type")),
						cursor.getString(cursor.getColumnIndex("schedule_content")),
						cursor.getString(cursor.getColumnIndex("schedule_date")),
						cursor.getString(cursor.getColumnIndex("schedule_opration")),
						cursor.getString(cursor.getColumnIndex("schedule_status")),
						cursor.getString(cursor.getColumnIndex("schedule_start_date")),
						cursor.getString(cursor.getColumnIndex("schedule_start_time")),
						cursor.getString(cursor.getColumnIndex("schedule_end_date")),
						cursor.getString(cursor.getColumnIndex("schedule_end_time")),
						cursor.getString(cursor.getColumnIndex("schedule_mode")),
						cursor.getString(cursor.getColumnIndex("schedule_objectId")),
						cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));

				scheduleList.add(o);
			}
			cursor.close();
			db.close();
          return scheduleList;
	}
	
	/**
	 * 保存日程信息
	 * @param scheduleVO
	 */
	public int save(ScheduleVO scheduleVO){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("scheduleTypeID", scheduleVO.getScheduleTypeID());
		values.put("remindID", scheduleVO.getRemindID());
		values.put("schedule_title", scheduleVO.getScheduleTitle());
		values.put("schedule_type", scheduleVO.getScheduleType());
		values.put("schedule_content", scheduleVO.getScheduleContent());
		values.put("schedule_date", scheduleVO.getScheduleDate());
		values.put("schedule_opration", scheduleVO.getScheduleOpration());
		values.put("schedule_status","0");
		values.put("schedule_start_date",scheduleVO.getScheduleStartDate().toString());
		values.put("schedule_start_time",scheduleVO.getScheduleStartTime().toString());
		values.put("schedule_end_date",scheduleVO.getScheduleEndDate().toString());
		values.put("schedule_end_time",scheduleVO.getScheduleEndTime().toString());
		values.put("schedule_mode",scheduleVO.getScheduleMode());
		values.put("schedule_objectId",scheduleVO.getScheduleObjectID());
		values.put("schedule_cycle_type",scheduleVO.getScheduleCycleType());
		int scheduleID = -1;
		try{
			db.insert("t_schedule", null, values);
		    Cursor cursor = db.rawQuery("select max(schedule_id) from t_schedule", null);
		    if(cursor.moveToFirst()){
		    	scheduleID = (int) cursor.getLong(0);
		    }
		    cursor.close();
		}finally{
			db.close();
		}
	    return scheduleID;
	}
	
	public void resetAllRunningScheduleWhenServiceStart(String opration){
		String sql="update t_schedule set schedule_status ='0' where schedule_status='1' and schedule_opration='"+opration+"'";
		SQLiteDatabase db = manager.getWritableDatabase();
		try {
			db.execSQL(sql);
		} finally{
			db.close();
		}
	}
	
	public void resetScheduleStatusFlags(int scheduleID,String flag){
		String sql="update t_schedule set schedule_status ='"+flag+"' where schedule_id=" + scheduleID;
		SQLiteDatabase db = manager.getWritableDatabase();
		try {
			db.execSQL(sql);
		} finally{
		}
		db.close();
	}
	
	public void resetScheduleStatusFlagsToFinished(int scheduleID){
		String sql="update t_schedule set schedule_status ='2' where schedule_id=" + scheduleID;
		SQLiteDatabase db = manager.getWritableDatabase();
		try {
			db.execSQL(sql);
		} finally{
			db.close();
		}
	}
	
	
	/**
	 * 查询某一条日程信息
	 * @param scheduleID
	 * @return
	 */
	public ScheduleVO getScheduleByID(int scheduleID){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), "schedule_id=?", new String[]{String.valueOf(scheduleID)}, null, null, null);
		if(cursor.moveToFirst()){
			int schID = cursor.getInt(cursor.getColumnIndex("schedule_id"));
			int scheduleTypeID = cursor.getInt(cursor.getColumnIndex("scheduleTypeID"));
			int remindID = cursor.getInt(cursor.getColumnIndex("remindID"));
			String scheduleTitle= cursor.getString(cursor.getColumnIndex("schedule_title"));
			int scheduleType=0;
			String scheduleContent=cursor.getString(cursor.getColumnIndex("schedule_content"));
			String scheduleDate= cursor.getString(cursor.getColumnIndex("schedule_date"));
			String scheduleOpration="";
			String scheduleStatus= cursor.getString(cursor.getColumnIndex("schedule_status"));
			String scheduleStartDate=cursor.getString(cursor.getColumnIndex("schedule_start_date"));
			String scheduleStartTime=cursor.getString(cursor.getColumnIndex("schedule_start_time"));
			String scheduleEndDate=cursor.getString(cursor.getColumnIndex("schedule_end_date"));
			String scheduleEndTime=cursor.getString(cursor.getColumnIndex("schedule_end_time"));
			String scheduleMode="";
			String scheduleObjectID="";
			String scheduleCycleType="";
			cursor.close();
			db.close();
			return new ScheduleVO(schID, scheduleTypeID, remindID, scheduleTitle, scheduleType, scheduleContent, scheduleDate, scheduleOpration, scheduleStatus, scheduleStartDate, scheduleStartTime, scheduleEndDate, scheduleEndTime, scheduleMode, scheduleObjectID, scheduleCycleType);
		}
		cursor.close();
		db.close();
		return null;
		
	}
	
	/**
	 * 查询所有非SMS日程信息
	 * 
	 * @return
	 */
	public List<ScheduleVO> getAllScheduleWithOperationRemind(){
		//manager = new manager(context, "schedules.db");
		List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getWritableDatabase();
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), "schedule_opration=? and schedule_status=?", new String[]{"remind","0"}, null, null, null);
		while(cursor.moveToNext()){
			ScheduleVO o=new ScheduleVO(
					cursor.getInt(cursor.getColumnIndex("schedule_id")),
					cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
					cursor.getInt(cursor.getColumnIndex("remindID")),
					cursor.getString(cursor.getColumnIndex("schedule_title")),
					cursor.getInt(cursor.getColumnIndex("schedule_type")),
					cursor.getString(cursor.getColumnIndex("schedule_content")),
					cursor.getString(cursor.getColumnIndex("schedule_date")),
					cursor.getString(cursor.getColumnIndex("schedule_opration")),
					cursor.getString(cursor.getColumnIndex("schedule_status")),
					cursor.getString(cursor.getColumnIndex("schedule_start_date")),
					cursor.getString(cursor.getColumnIndex("schedule_start_time")),
					cursor.getString(cursor.getColumnIndex("schedule_end_date")),
					cursor.getString(cursor.getColumnIndex("schedule_end_time")),
					cursor.getString(cursor.getColumnIndex("schedule_mode")),
					cursor.getString(cursor.getColumnIndex("schedule_objectId")),
					cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));

			scheduleList.add(o);
		}
		cursor.close();
		db.close();
		return scheduleList;
	}
	
	/**
	 * 查询所有运行中的提醒日程信息
	 * 
	 * @return
	 */
	public List<ScheduleVO> getAllScheduleInRuning(){
		//manager = new manager(context, "schedules.db");
		List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getWritableDatabase();
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), " schedule_status=? and schedule_opration=?", new String[]{"1","remind"}, null, null, null);
		while(cursor.moveToNext()){
			ScheduleVO o=new ScheduleVO(
					cursor.getInt(cursor.getColumnIndex("schedule_id")),
					cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
					cursor.getInt(cursor.getColumnIndex("remindID")),
					cursor.getString(cursor.getColumnIndex("schedule_title")),
					cursor.getInt(cursor.getColumnIndex("schedule_type")),
					cursor.getString(cursor.getColumnIndex("schedule_content")),
					cursor.getString(cursor.getColumnIndex("schedule_date")),
					cursor.getString(cursor.getColumnIndex("schedule_opration")),
					cursor.getString(cursor.getColumnIndex("schedule_status")),
					cursor.getString(cursor.getColumnIndex("schedule_start_date")),
					cursor.getString(cursor.getColumnIndex("schedule_start_time")),
					cursor.getString(cursor.getColumnIndex("schedule_end_date")),
					cursor.getString(cursor.getColumnIndex("schedule_end_time")),
					cursor.getString(cursor.getColumnIndex("schedule_mode")),
					cursor.getString(cursor.getColumnIndex("schedule_objectId")),
					cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));
			scheduleList.add(o);
		}
		cursor.close();
		db.close();
		return scheduleList;
	}
	
	/**
	 * 查询所有运行中的短信日程信息
	 * 
	 * @return
	 */
	public List<ScheduleVO> getAllSMSScheduleInRuning(){
		//manager = new manager(context, "schedules.db");
		List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getWritableDatabase();
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), " schedule_status=? and schedule_opration=?", new String[]{"1","sms"}, null, null, null);
		while(cursor.moveToNext()){
			ScheduleVO o=new ScheduleVO(
					cursor.getInt(cursor.getColumnIndex("schedule_id")),
					cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
					cursor.getInt(cursor.getColumnIndex("remindID")),
					cursor.getString(cursor.getColumnIndex("schedule_title")),
					cursor.getInt(cursor.getColumnIndex("schedule_type")),
					cursor.getString(cursor.getColumnIndex("schedule_content")),
					cursor.getString(cursor.getColumnIndex("schedule_date")),
					cursor.getString(cursor.getColumnIndex("schedule_opration")),
					cursor.getString(cursor.getColumnIndex("schedule_status")),
					cursor.getString(cursor.getColumnIndex("schedule_start_date")),
					cursor.getString(cursor.getColumnIndex("schedule_start_time")),
					cursor.getString(cursor.getColumnIndex("schedule_end_date")),
					cursor.getString(cursor.getColumnIndex("schedule_end_time")),
					cursor.getString(cursor.getColumnIndex("schedule_mode")),
					cursor.getString(cursor.getColumnIndex("schedule_objectId")),
					cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));
			scheduleList.add(o);
		}
		cursor.close();
		db.close();
		return scheduleList;
	}

	
	/**
	 * 查询所有SMS日程信息
	 * 
	 * @return
	 */
	public List<ScheduleVO> getAllScheduleWithOperationSMS(){
		//manager = new manager(context, "schedules.db");
		List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getWritableDatabase();
		while(db.isDbLockedByOtherThreads()||db.isDbLockedByCurrentThread()){
			try {
				Thread.sleep(60*1*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), "schedule_opration=? and schedule_status=?", new String[]{"sms","0"}, null, null, null);
		while(cursor.moveToNext()){
			ScheduleVO o=new ScheduleVO(
					cursor.getInt(cursor.getColumnIndex("schedule_id")),
					cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
					cursor.getInt(cursor.getColumnIndex("remindID")),
					cursor.getString(cursor.getColumnIndex("schedule_title")),
					cursor.getInt(cursor.getColumnIndex("schedule_type")),
					cursor.getString(cursor.getColumnIndex("schedule_content")),
					cursor.getString(cursor.getColumnIndex("schedule_date")),
					cursor.getString(cursor.getColumnIndex("schedule_opration")),
					cursor.getString(cursor.getColumnIndex("schedule_status")),
					cursor.getString(cursor.getColumnIndex("schedule_start_date")),
					cursor.getString(cursor.getColumnIndex("schedule_start_time")),
					cursor.getString(cursor.getColumnIndex("schedule_end_date")),
					cursor.getString(cursor.getColumnIndex("schedule_end_time")),
					cursor.getString(cursor.getColumnIndex("schedule_mode")),
					cursor.getString(cursor.getColumnIndex("schedule_objectId")),
					cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));
			scheduleList.add(o);
		}
		cursor.close();
		db.close();
		return scheduleList;
	}
	
	/**
	 * 查询所有月和年的日程信息
	 * 
	 * @return
	 */
	public List<ScheduleVO> getAllScheduleCycleMonthOrYear(){
		//manager = new manager(context, "schedules.db");
		List<ScheduleVO> scheduleList =new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getWritableDatabase();
		while(db.isDbLockedByOtherThreads()||db.isDbLockedByCurrentThread()){
			try {
				Thread.sleep(60*1*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Cursor cursor = db.query("t_schedule",formatSechduleColumn(), 
				"(schedule_status=? and schedule_cycle_type='M' or schedule_status=? and schedule_cycle_type='Y' ) or (schedule_status=? and schedule_cycle_type='M' or schedule_status=? and schedule_cycle_type='Y' ) ", 
				new String[]{"0","0","1","1"}, null, null, null);
		while(cursor.moveToNext()){
			ScheduleVO o=new ScheduleVO(
					cursor.getInt(cursor.getColumnIndex("schedule_id")),
					cursor.getInt(cursor.getColumnIndex("scheduleTypeID")), 
					cursor.getInt(cursor.getColumnIndex("remindID")),
					cursor.getString(cursor.getColumnIndex("schedule_title")),
					cursor.getInt(cursor.getColumnIndex("schedule_type")),
					cursor.getString(cursor.getColumnIndex("schedule_content")),
					cursor.getString(cursor.getColumnIndex("schedule_date")),
					cursor.getString(cursor.getColumnIndex("schedule_opration")),
					cursor.getString(cursor.getColumnIndex("schedule_status")),
					cursor.getString(cursor.getColumnIndex("schedule_start_date")),
					cursor.getString(cursor.getColumnIndex("schedule_start_time")),
					cursor.getString(cursor.getColumnIndex("schedule_end_date")),
					cursor.getString(cursor.getColumnIndex("schedule_end_time")),
					cursor.getString(cursor.getColumnIndex("schedule_mode")),
					cursor.getString(cursor.getColumnIndex("schedule_objectId")),
					cursor.getString(cursor.getColumnIndex("schedule_cycle_type")));
			scheduleList.add(o);
		}
		cursor.close();
		db.close();
		return scheduleList;
	}
	
	/**
	 * 更新开始时间为下一个周期
	 * 
	 * @return
	 */
	
	public void updateMAndYSchedleStartDate(Integer id,String time){
		String sql="update t_schedule set schedule_start_date ='"+time+"' where schedule_id=" + id;
		SQLiteDatabase db = manager.getWritableDatabase();
		try {
			db.execSQL(sql);
		} finally{
			db.close();
		}
	}
	
	public String[] formatSechduleColumn(){
		String[] columnArr=new String[DBInitScript.schedule_column.size()];
		Iterator iter = DBInitScript.schedule_column.entrySet().iterator();
		int i=0;
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = entry.getKey().toString();
			columnArr[i]=key;
			i++;
		}
		return columnArr;
	}
	
	/**
	 * 查询所有的日程信息
	 * @return
	 */
	public ArrayList<ScheduleVO> getAllSchedule(){
		ArrayList<ScheduleVO> list = new ArrayList<ScheduleVO>();
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getReadableDatabase();
		Cursor cursor = db.query("t_schedule", new String[]{"schedule_id","scheduleTypeID","remindID","schedule_content","schedule_date"}, null, null, null, null, "schedule_id desc");
		while(cursor.moveToNext()){
			int scheduleID = cursor.getInt(cursor.getColumnIndex("schedule_id")); 
			int scheduleTypeID = cursor.getInt(cursor.getColumnIndex("scheduleTypeID"));
			int remindID = cursor.getInt(cursor.getColumnIndex("remindID"));
			String scheduleContent = cursor.getString(cursor.getColumnIndex("schedule_content"));
			String scheduleDate = cursor.getString(cursor.getColumnIndex("schedule_date"));
			ScheduleVO vo = new ScheduleVO(scheduleID,scheduleTypeID,remindID,scheduleContent,scheduleDate);
			list.add(vo);
		}
		cursor.close();
		db.close();
		if(list != null && list.size() > 0){
			return list;
		}
		return null;
		
	}
	
	/**
	 * 删除日程
	 * @param scheduleID
	 */
	public void delete(int scheduleID){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		try{
			db.delete("t_schedule", "schedule_id=?", new String[]{String.valueOf(scheduleID)});
			db.delete("t_schedule_tag_date", "scheduleID=?", new String[]{String.valueOf(scheduleID)});
		}finally{
		}
		db.close();
	}
	
	
	public void clearAllFinishSchadule(){
		List<Integer> list=findAllFinishScheduleID();
		if(null!=list){
			SQLiteDatabase db = manager.getWritableDatabase();
			try{
				for (int i = 0; i < list.size(); i++) {
					db.delete("t_schedule", "schedule_id=?", new String[]{String.valueOf(list.get(i))});
					db.delete("t_schedule_tag_date", "scheduleID=?", new String[]{String.valueOf(list.get(i))});
				}
			}finally{
				db.close();
			}
		}
		
	}
	
	
	public List<Integer> findAllFinishScheduleID(){
		List<Integer> list =new ArrayList<Integer>();
		SQLiteDatabase db = manager.getReadableDatabase();
		Cursor cursor = db.query("t_schedule", new String[]{"schedule_id"},"schedule_status = 2", null, null, null, "schedule_id desc");
		while(cursor.moveToNext()){
			int scheduleID = cursor.getInt(cursor.getColumnIndex("schedule_id")); 
						list.add(scheduleID);
		}
		cursor.close();
		db.close();
		if(list != null && list.size() > 0){
			return list;
		}
		return null;
	}
	
	
	/**
	 * 更新日程
	 * @param vo
	 */
	public int update(ScheduleVO scheduleVO){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
			String sql= " update t_schedule set scheduleTypeID="+scheduleVO.getScheduleTypeID()+
					    " ,remindID ="+scheduleVO.getRemindID()+
					    " ,schedule_title='"+scheduleVO.getScheduleTitle()+"'"+
					    " ,schedule_type="+scheduleVO.getScheduleType()+
					    " ,schedule_content='"+scheduleVO.getScheduleContent()+"'"+
					    " ,schedule_date='"+scheduleVO.getScheduleDate()+"'"+
					    " ,schedule_opration='"+ scheduleVO.getScheduleOpration()+"'"+
					    " ,schedule_status='0'"+
					    " ,schedule_start_date='"+scheduleVO.getScheduleStartDate().toString()+"'"+
					    " ,schedule_start_time='"+scheduleVO.getScheduleStartTime().toString()+"'"+
					    " ,schedule_end_date='"+scheduleVO.getScheduleEndDate().toString()+"'"+
					    " ,schedule_end_time='"+scheduleVO.getScheduleEndTime().toString()+"'"+
					    " ,schedule_mode='"+scheduleVO.getScheduleMode().toString()+"'"+
					    " ,schedule_objectId ="+scheduleVO.getScheduleObjectID()+
					    " ,schedule_cycle_type='"+scheduleVO.getScheduleCycleType()+"'"+
					    " where schedule_id='"+scheduleVO.getScheduleID()+"'";
			db.execSQL(sql);
//			db.update("t_schedule", values, "schedule_id=?", new String[]{String.valueOf(scheduleVO.getScheduleID())});
			db.close();
		return scheduleVO.getScheduleID();
	}
	
	/**
	 * 将日程标志日期保存到数据库中
	 * @param dateTagList
	 */
	public void updateTagDate(ArrayList<ScheduleDateTag> dateTagList){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		ScheduleDateTag dateTag = new ScheduleDateTag();
		for(int i = 0; i < dateTagList.size(); i++){
			dateTag = dateTagList.get(i);
			ContentValues values = new ContentValues();
			values.put("year", dateTag.getYear());
			values.put("month", dateTag.getMonth());
			values.put("day", dateTag.getDay());
//			values.put("scheduleID", dateTag.getScheduleID());
			db.update("t_schedule_tag_date", values, "scheduleID=?", new String[]{String.valueOf(dateTag.getScheduleID())});
//			db.insert("t_schedule_tag_date", null, values);
		}
		db.close();
	}
	
	/**
	 * 将日程标志日期保存到数据库中
	 * @param dateTagList
	 */
	public void saveTagDate(ArrayList<ScheduleDateTag> dateTagList){
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getWritableDatabase();
		ScheduleDateTag dateTag = new ScheduleDateTag();
		for(int i = 0; i < dateTagList.size(); i++){
			dateTag = dateTagList.get(i);
			ContentValues values = new ContentValues();
			values.put("year", dateTag.getYear());
			values.put("month", dateTag.getMonth());
			values.put("day", dateTag.getDay());
			values.put("scheduleID", dateTag.getScheduleID());
			db.insert("t_schedule_tag_date", null, values);
		}
		db.close();
	}
	
	/**
	 * 只查询出当前月的日程日期
	 * @param currentYear
	 * @param currentMonth
	 * @return
	 */
	public ArrayList<ScheduleDateTag> getTagDate(int currentYear, int currentMonth){
		ArrayList<ScheduleDateTag> dateTagList = new ArrayList<ScheduleDateTag>();
		//manager = new manager(context, "schedules.db");
		SQLiteDatabase db = manager.getReadableDatabase();
		Cursor cursor = db.query("t_schedule_tag_date", new String[]{"tag_id","year","month","day","scheduleID"}, "year=? and month=?", new String[]{String.valueOf(currentYear),String.valueOf(currentMonth)}, null, null, null);
		while(cursor.moveToNext()){
			int tagID = cursor.getInt(cursor.getColumnIndex("tag_id"));
			int year = cursor.getInt(cursor.getColumnIndex("year"));
			int month = cursor.getInt(cursor.getColumnIndex("month"));
			int day = cursor.getInt(cursor.getColumnIndex("day"));
			int scheduleID = cursor.getInt(cursor.getColumnIndex("scheduleID"));
			ScheduleDateTag dateTag = new ScheduleDateTag(tagID,year,month,day,scheduleID);
			dateTagList.add(dateTag);
			}
		cursor.close();
		if(dateTagList != null && dateTagList.size() > 0){
			return dateTagList;
		}
		return null;
	}
	
	/**
	 * 当点击每一个gridview中item时,查询出此日期上所有的日程标记(scheduleID)
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public String[] getScheduleByTagDate(int year, int month, int day,int scheduleStatus){
		ArrayList<ScheduleVO> scheduleList = new ArrayList<ScheduleVO>();
		SQLiteDatabase db = manager.getReadableDatabase();
		//根据时间查询出日程ID（scheduleID），一个日期可能对应多个日程ID
		Cursor cursor;
		if(scheduleStatus!=3){
			cursor=db.query("t_schedule_tag_date,t_schedule", new String[]{"scheduleID"}, "year=? and month=? and day=? and t_schedule_tag_date.scheduleID =t_schedule.schedule_id and t_schedule.schedule_status=?", new String[]{String.valueOf(year),String.valueOf(month),String.valueOf(day),String.valueOf(scheduleStatus)}, null, null, null);
		}else{
			cursor=db.query("t_schedule_tag_date", new String[]{"scheduleID"}, "year=? and month=? and day=? ", new String[]{String.valueOf(year),String.valueOf(month),String.valueOf(day)}, null, null, null);
		}
		String scheduleIDs[] = null;
		scheduleIDs = new String[cursor.getCount()];
		int i = 0;
		while(cursor.moveToNext()){
			String scheduleID = cursor.getString(cursor.getColumnIndex("scheduleID"));
			scheduleIDs[i] = scheduleID;
			i++;
		}
		cursor.close();
		db.close();
		return scheduleIDs;
		
		
	}
	
	/**
	 *关闭DB
	 */
	public void destoryDB(){
		if(manager != null){
			manager.close();
		}
	}
	
	
}
